import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '@/views/LoginView.vue'
import RegisteView from '@/views/RegisteView.vue'
import MainFrameView from '@/views/MainFrameView.vue'
import indexpage from '@/views/indexpage.vue'
import BuildingPage from '../views/BuildingPage.vue'
import UserinfoPage from '@/views/UserinfoPage.vue'
import MenuView from '@/views/MenuView.vue'
import RoleView from '@/views/RoleView.vue'
import houseView from '@/views/houseView.vue'
//引入useCounterStore实例
import { useUserStore } from '../stores/userStore'



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'LoginView',
      component: LoginView,
    },
    {
      path: '/RegisteView',
      name: 'RegisteView',
      component: RegisteView,
    },
    {
      path: '/MainFrameView',
      name: 'MainFrameView',
      component: MainFrameView,
      redirect: '/MainFrameView/indexpage',
      children: [
        {
          path: '/MainFrameView/indexpage',
          name: 'indexpage',
          component: indexpage,
        },
        {
          path: '/BuildingPage',
          name: 'BuildingPage',
          component: BuildingPage,
        },
        {
          path: '/UserinfoPage',
          name: 'UserinfoPage',
          component: UserinfoPage,
        },
        {
          path: '/MenuView',
          name: 'MenuView',
          component: MenuView,
        },
        {
          path: '/Roles',
          name: 'RoleView',
          component: RoleView,
        },
        {
          path: '/HousePage',
          name: 'houseView',
          component: houseView,
        },
      ]
    },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue'),
    // },
  ],
})
//添加路由守卫
/**
 * to:表示一个对象， 即将跳转的路由对象
 * from: 路由跳转之前对象
 * next: 是一个函数 ，通过next设置跳转路由，终止|放行路由跳转
 */
router.beforeEach((to, from, next) => {
  //创建stote实例
  let store = useUserStore();
  if (to.path == '/' || to.path == '/RegisteView') {
    next();
  } else {
    if (store.userinfo.username) {
      next();
    } else {
      console.log('路由守卫跳转登录界面');

      next('/');
    }
  }
})

export default router
